﻿using ShoppingCar.Bottoms;
using ShoppingCar.botton;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ShoppingCar
{
    public class ButtomAppService : ApplicationService, IButtomService
    {
        private readonly IRepository<Bottom, int> _bottoms;
        public ButtomAppService(IRepository<Bottom, int> bottoms)
        {
            _bottoms = bottoms;
        }
        //添加
        public int CreateButtom(CreateUpdateButtomDto dto)
        {
            var add = new Bottom()
            {
                BottomnavigationName=dto.BottomnavigationName,
                Headskip=dto.Headskip,
                HeadOrder=dto.HeadOrder,
                HeadState=dto.HeadState
            };
            _bottoms.InsertAsync(add);
            return 10;
        }
        //删除
        public int DeleteButtom(int id)
        {
            _bottoms.DeleteAsync(id);
            return 1;
        }
        //反填
        public ButtomDto Fantian(int id)
        {
            Bottom bottom = _bottoms.Where(s => s.Id.Equals(id)).FirstOrDefault();
            return ObjectMapper.Map<Bottom, ButtomDto>(bottom);
        }
        //修改
        public int UpdateButtom(int id, CreateUpdateButtomDto dto)
        {
            Bottom headss = _bottoms.FirstOrDefault(s => s.Id.Equals(id));
            headss.BottomnavigationName = dto.BottomnavigationName;
            headss.HeadOrder = dto.HeadOrder;
            headss.HeadState = dto.HeadState;
            _bottoms.UpdateAsync(headss);
            return 10;
        }
        //显示
        public List<ButtomListDto> GetButtomLists()
        {
            var get = from a in _bottoms
                      select new ButtomListDto
                      {
                          Id = a.Id,
                          BottomnavigationName = a.BottomnavigationName,
                          Headskip = a.Headskip,
                          HeadOrder = a.HeadOrder,
                          HeadState = a.HeadState
                      };
            var list = get.ToList();
            return list;
        }
        //状态修改
        public ButtomDto UpdateState(int id)
        {
            Bottom bottom1 = _bottoms.FirstOrDefault(s => s.Id.Equals(id));
            if (bottom1.HeadState==1)
            {
                bottom1.HeadState = 0;
            }
            else
            {
                bottom1.HeadState = 1;
            }
             return ObjectMapper.Map<Bottom, ButtomDto>(bottom1);
        }
    }
}
